package com.cdks.wuxiu.api.controller;

import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.model.common.AuthParam;
import com.cdks.wuxiu.api.model.param.RechargeOrderParam;
import com.cdks.wuxiu.api.model.param.RechargeGetRecordParam;
import com.cdks.wuxiu.api.model.vo.RechargeAlipayVO;
import com.cdks.wuxiu.api.model.vo.RechargeApplepayVO;
import com.cdks.wuxiu.api.model.vo.RechargeWxpayVO;
import com.cdks.wuxiu.api.service.RechargeService;
import com.cdks.wuxiu.api.util.AlipayUtils;
import com.cdks.wuxiu.api.util.WxpayUtils;
import com.cdks.wuxiu.common.model.api.ApiResult;
import com.cdks.wuxiu.entity.mysql.RechargeOrderInfo;
import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.Map;


@Slf4j
@Api(tags = "充值")
@RestController
@RequestMapping("/recharge")
public class RechargeController {
    @Resource
    private RechargeService rechargeService;


    @ApiOperation("获取充值金额列表")
    @PostMapping("/getPriceList")
    public ApiResult<Object> getPriceList(@RequestBody @Valid AuthParam param) {
        JSONObject json  = new JSONObject();
        json.set("priceList", rechargeService.getPriceList(param.getBranch()));
        
        return ApiResult.success(json);
    }

    @ApiOperation("创建支付宝订单")
    @PostMapping("/alipay")
    public ApiResult<RechargeAlipayVO> alipay(@RequestBody @Valid RechargeOrderParam param) {
        if (param.getUserInfo().getType() == Constant.ACCOUNT_TYPE_KEFU)
        {
            return ApiResult.failed(10, "无权限");
        }

        RechargeOrderInfo order = rechargeService.createOrder(param.getUserId(), param.getBranch(), param.getPriceId(), Constant.PAY_TYPE_ALIPAY);
        if (order == null)
        {
            return ApiResult.failed(10, "创建订单失败");
        }

        String alipaySign  = AlipayUtils.appPay(order.getOrderNo(), order.getMoney(), "账户充值");
        RechargeAlipayVO vo = new RechargeAlipayVO();
        vo.setAlipaySign(alipaySign);

        return ApiResult.success(vo);
    }

    @ApiOperation("创建微信订单")
    @PostMapping("/wxpay")
    public ApiResult<RechargeWxpayVO> wxpay(@RequestBody @Valid RechargeOrderParam param) {
        if (param.getUserInfo().getType() == Constant.ACCOUNT_TYPE_KEFU)
        {
            return ApiResult.failed(10, "无权限");
        }

        RechargeOrderInfo order = rechargeService.createOrder(param.getUserId(), param.getBranch(), param.getPriceId(), Constant.PAY_TYPE_WXPAY);
        if (order == null)
        {
            return ApiResult.failed(10, "创建订单失败");
        }

        Map<String, String> wxpaySign  = WxpayUtils.appPay(order.getOrderNo(), order.getMoney(), "账户充值");
        RechargeWxpayVO vo = new RechargeWxpayVO();
        vo.setWxpaySign(wxpaySign);

        return ApiResult.success(vo);
    }

    @ApiOperation("创建苹果支付订单")
    @PostMapping("/applepay")
    public ApiResult<RechargeApplepayVO> applepay(@RequestBody @Valid RechargeOrderParam param) {
        if (param.getUserInfo().getType() == Constant.ACCOUNT_TYPE_KEFU)
        {
            return ApiResult.failed(10, "无权限");
        }

        RechargeOrderInfo order = rechargeService.createOrder(param.getUserId(), param.getBranch(), param.getPriceId(), Constant.PAY_TYPE_APPLE);
        if (order == null)
        {
            return ApiResult.failed(10, "创建订单失败");
        }

        RechargeApplepayVO vo = new RechargeApplepayVO();
        vo.setOrderNo(order.getOrderNo());

        return ApiResult.success(vo);
    }

    @ApiOperation("获取充值记录")
    @PostMapping("/getRecord")
    public ApiResult<Object> getRecord(@RequestBody @Valid RechargeGetRecordParam param) {       
        JSONObject json  = new JSONObject();
        json.set("recordList", rechargeService.getRecord(param.getUserId(), param.getPage(), param.getStart(), param.getStop()));

        return ApiResult.success(json);
    }
}
